package com.ygl.strategy.service.sentinel.parser;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.ygl.common.constant.DiscoveryConstant;
import com.ygl.strategy.service.sentinel.adapter.ServiceSentinelRequestOriginAdapter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author yanggld
 * @Date 2020/1/10-17:48
 * @Description
 */
public class ServiceSentinelRequestOriginParser implements RequestOriginParser {

	@Autowired(required = false)
	private ServiceSentinelRequestOriginAdapter serviceSentinelRequestOriginAdapter;

	public ServiceSentinelRequestOriginParser() {
		WebCallbackManager.setRequestOriginParser(this);
	}

	@Override
	public String parseOrigin(javax.servlet.http.HttpServletRequest request) {
		if (serviceSentinelRequestOriginAdapter != null) {
			return serviceSentinelRequestOriginAdapter.parseOrigin(request);
		} else {
			String requestOriginValue = request.getHeader("origin");
			return StringUtils.isNotEmpty(requestOriginValue) ? requestOriginValue : "";
		}
	}
}
